﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;

namespace Iacym.UI
{
    public partial class FrmSexo : Form
    {
        [Dependency]
        public ISexoBL sexoBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmSexo()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public USUARIO currentUsuarioSexo { get; set; }

        private void FrmSexo_Load(object sender, EventArgs e)
        {
            if (currentUsuarioSexo != null)
            {
                menuLlena();
            }
            else
                CargarDatos();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioSexo.PERSONA.Dni);

            var permisos = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.UsuarioId)
                            select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[] { 
            this.BtnEliminar,this.BtnEditar,this.BtnNuevo};

            var noVisible = from nv in botones
                            where !permisos.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargarDatos();
        }

        private void CargarDatos()
        {
            sEXOBindingSource.DataSource = sexoBL.GetAllFromSexo();
        }

        private void BtnEliminar_Click(object sender, EventArgs e)
        {
            SEXO currentsexo = sEXOBindingSource.Current as SEXO;

            if (currentsexo != null)
            {
                if (currentsexo.Id > 0 && currentsexo.Id < 3)
                {
                    MessageBox.Show(this,
                       string.Format("No se puede eliminar el Sexo: {0}", currentsexo.Descripcion),
                       "Mensaje",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                }
                else
                {
                    var rpt = MessageBox.Show(this,
                      string.Format("¿Desea Eliminar el Sexo: {0}?", currentsexo.Descripcion),
                      "Mensaje",
                      MessageBoxButtons.YesNo,
                      MessageBoxIcon.Question);

                    if (rpt == DialogResult.Yes)
                    {
                        sexoBL.DeleteSexo(currentsexo.Id);
                        CargarDatos();
                    }
                }
            }
            else
            {
                MessageBox.Show("Debe Seleccionar un Sexo.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnEditar_Click(object sender, EventArgs e)
        {
            SEXO currentsexo = sEXOBindingSource.Current as SEXO;

            if (currentsexo != null)
            {
                if (currentsexo.Id > 0 && currentsexo.Id < 3)
                {
                    MessageBox.Show(this,
                       string.Format("No se puede editar el Sexo: {0}", currentsexo.Descripcion),
                       "Mensaje",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                }
                else
                {
                    FrmMttoSexo mtto = new FrmMttoSexo();
                    mtto.currentSexo = currentsexo;
                    mtto.Editar();
                    mtto.ShowDialog();
                    CargarDatos();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Sexo.", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoSexo mtto = new FrmMttoSexo();            
            mtto.Nuevo();
            mtto.ShowDialog();
            CargarDatos();
        }

        private void TxtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void BtnBuscar_Click(object sender, EventArgs e)
        {
            sEXOBindingSource.DataSource = sexoBL.GetAllFromSexoByDescripcion(TxtDescripcion.Text);
        }

        private void TxtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                sEXOBindingSource.DataSource = sexoBL.GetAllFromSexoByDescripcion(TxtDescripcion.Text);
            }
        }
    }
}
